package com.seeyon.apps.kk.manager;

import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.seeyon.ctp.organization.bo.V3xOrgMember;
import com.seeyon.ctp.organization.event.AddMemberEvent;
import com.seeyon.ctp.util.annotation.ListenEvent;
import com.seeyon.kk.utils.LdapUtils;
import com.seeyon.ctp.organization.event.ChangePwdEvent;

public class KKADSyncManager extends BaseManager{

	private static final Log log = LogFactory.getLog(KKADSyncManager.class);
	public String group; // xml 配置安全组名称

	public String getGroup() {
		return group;
	}

	public void setGroup(String group) {
		this.group = group;
	}

	public LdapUtils getLdapUtils() {
		return ldapUtils;
	}

	public void setLdapUtils(LdapUtils ldapUtils) {
		this.ldapUtils = ldapUtils;
	}

	private LdapUtils ldapUtils;
	
	public Map<String, String> adSync() {
		Map<String, String> rMap = new HashMap<>();
		List<Map<String, Object>> dept = this.ibatisQueryDao.queryForList("getDeptAll_select_lockkk", null);
		List<Map<String, Object>> member = this.ibatisQueryDao.queryForList("getMemberAll_select_lockkk", null);
		/*List<Map<String, Object>> groupMap = this.ibatisQueryDao.queryForList("group_select_lockkk", null);*/
		List<Map<String, Object>> updateMember = this.ibatisQueryDao.queryForList("getUpdMember_select_lockkk", null);

		if (ldapUtils.updateAdDept(dept)) {
			rMap.put("code", "1000");
			rMap.put("result", "updateAdDept succ;");
		} else {
			rMap.put("code", "1001");
			rMap.put("result", "updateAdDept error;");
		}
		if (ldapUtils.addAdDept(dept)) {
			rMap.put("code", "1000");
			rMap.put("result", "addAdDept succ;");
		} else {
			rMap.put("code", "1001");
			rMap.put("result", "addAdDept error;");
		}
		/* 安全组初始化
		 * if (ldapUtils.addGroup(groupMap, group)) { rMap.put("code", "1000");
		 * rMap.put("result", "addGroup succ;"); } else { rMap.put("code",
		 * "1001"); rMap.put("result", "addGroup error;"); }*/
		if (ldapUtils.updateMember(updateMember, dept.get(0).get("name").toString(), group)) {
			rMap.put("code", "1000");
			rMap.put("result", "updateMember succ;");
		} else {
			rMap.put("code", "1001");
			rMap.put("result", "updateMember error;");
		}
		rMap = ldapUtils.addMember(member, dept.get(0).get("name").toString(), group);
		if (rMap.get("code").equals("1001")) {
			return rMap;
		} 
		if (ldapUtils.updateDisOrOther(member, dept.get(0).get("name").toString())) {
			rMap.put("code", "1000");
			rMap.put("result", "updateDisOrOther succ;");
		} else {
			rMap.put("code", "1001");
			rMap.put("result", "updateDisOrOther error;");
		}
		// 初始化方法
		/*
		 * if (ldapUtils.builders(member, dept.get(0).get("name").toString())) {
		 * rMap.put("code", "1000"); rMap.put("result", "updateMember succ;"); }
		 * else { rMap.put("code", "1001"); rMap.put("result",
		 * "updateMember error;"); }
		 */

		return rMap;
	}
	
	
	/**
	 * 监听人员新增事件
	 * 
	 * @param event
	 */
	@ListenEvent(event = AddMemberEvent.class, async = true)
	public void onMemberAdd(AddMemberEvent event) {
		try {
			Map<String, Object> mapBean = new HashMap<>();
			V3xOrgMember member = event.getMember();
			mapBean.put("id", member.getId());
			mapBean.put("login_name", member.getLoginName());
			mapBean.put("password", member.getPassword());
			mapBean.put("member_id", member.getId());
			mapBean.put("is_enable", member.getEnabled());
			SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			mapBean.put("create_time", format.format(member.getCreateTime()));
			mapBean.put("update_time", format.format(member.getUpdateTime()));
			this.ibatisQueryDao.getSqlMapClientTemplate().insert("userPwd_insert_lockkk", mapBean);
		} catch (Exception e) {
			log.error("insert temp_org_pri error:", e);
			log.error("insert temp_org_pri error:"+ e.getMessage());
		}
	}

	/**
	 * 监听人员密码事件
	 * 
	 * @param event
	 */
	@ListenEvent(event = ChangePwdEvent.class, async = true)
	public void onPasswordUpdate(ChangePwdEvent event) {
		try {
			Map<String, Object> mapBean = new HashMap<>();
			V3xOrgMember member = event.getMember();
			mapBean.put("id", member.getId());
			mapBean.put("login_name", member.getLoginName());
			mapBean.put("password", member.getPassword());
			SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			mapBean.put("update_time", format.format(member.getUpdateTime()));
			this.ibatisQueryDao.getSqlMapClientTemplate().update("userPwd_update_lockkk", mapBean);
			List<Map<String, Object>> dept = this.ibatisQueryDao.queryForList("getDeptAll_select_lockkk", null);
			if (ldapUtils.updatePwd(dept.get(0).get("name").toString(), member.getLoginName(), member.getPassword()))
				log.info("密码修改成功：" + member.getLoginName());
			else
				log.info("密码修改失败：" + member.getLoginName());
		} catch (Exception e) {
			log.info("Listen ChangePwdEvent error:"+e.getMessage());
		}
	}

}
